package bibliotecas;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JComboBoxLabel extends JPanel{

	private static final long serialVersionUID = 5723618247363371212L;
	
	public JLabel label = new JLabel();
	public JComboBox<String> comboBox = new JComboBox<String>();
	private int espacoEntreLabelETexto = 10;
	
	public JComboBoxLabel(String textoLabel,String dados[]){
		this.setLayout(null);
		
		label.setText(textoLabel);
		if(dados != null){
			for(int i = 0;i < dados.length;i++){
				comboBox.addItem(dados[i]);
			}
		}
		comboBox.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				comboBox.setBackground(Color.WHITE);
			}			
			@Override
			public void focusGained(FocusEvent e) {
				comboBox.setBackground(Color.LIGHT_GRAY);
			}
		});
		comboBox.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			@Override
			public void actionPerformed(ActionEvent arg0) {
				comboBox.nextFocus();
			}
		});
	}
	
	public void setEspacoEntreLabelEComboBox(int x){
		espacoEntreLabelETexto = x;
		setDimensoes(getX(),getY(),getWidth(),getHeight());
	}
	
	public String getItemSelecionado(){
		return comboBox.getSelectedItem().toString();
	}
	
	public void setDimensoes(int x,int y,int width,int height){
		int tamanhoLabel = espacoEntreLabelETexto + (label.getText().length() * 10);
		setBounds(x,y,width,height);
		label.setBounds(4,0,tamanhoLabel,height);
		add(label);
		comboBox.setBounds(tamanhoLabel,2,width - tamanhoLabel - 2,height - 4);
		add(comboBox);
	}

}
